#!/usr/bin/python
# -*- coding: utf-8 -*-
#@PydevCodeAnalysisIgnore

from os import environ
environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from settings import *

from apps.ad_manager.models import AdOrganize, AdOrganizeInfo
from apps.ad_usergroup.models import AdUserProfile, AdPlace
from django.contrib.auth.models import User, Permission, Group

import datetime


ADMIN_NAME = 'admin'
ADMIN_PASSWD = ADMIN_NAME
ADMIN_EMAIL = 'fla.sam@gmail.com'

print 'Staring initialization ......'

print 'Create Place items ......'
AD_PLACE = (
            ('中国', None), \
            ('北京市', '中国'), \
            ('上海市', '中国'), \
            ('天津市', '中国'), \
            ('重庆市', '中国'), \
            ('内蒙古', '中国'), \
            ('呼和浩特市', '内蒙古'), \
            ('河北省', '中国'), \
            ('石家庄市', '河北省'), \
            ('江西省', '中国'), \
            ('南昌市', '江西省'), \
            ('山东省', '中国'), \
            ('济南市', '山东省'), \
            ('山西省', '中国'), \
            ('太原市', '山西省'), \
            ('河南省', '中国'), \
            ('郑州市', '河南省'), \
            ('辽宁省', '中国'), \
            ('沈阳市', '辽宁省'), \
            ('湖北省', '中国'), \
            ('武汉市', '湖北省'), \
            ('吉林省', '中国'), \
            ('长春市', '吉林省'), \
            ('湖南省', '中国'), \
            ('长沙市', '湖南省'), \
            ('黑龙江省', '中国'), \
            ('哈尔滨市', '黑龙江省'), \
            ('广东省', '中国'), \
            ('广州市', '广东省'), \
            ('江苏省', '中国'), \
            ('南京市', '江苏省'), \
            ('广西省', '中国'), \
            ('南宁市', '广西省'), \
            ('海南省', '中国'), \
            ('海口市', '海南省'), \
            ('四川省', '中国'), \
            ('成都市', '四川省'), \
            ('浙江省', '中国'), \
            ('杭州市', '浙江省'), \
            ('贵州省', '中国'), \
            ('贵阳市', '贵州省'), \
            ('安徽省', '中国'), \
            ('合肥市', '安徽省'), \
            ('云南省', '中国'), \
            ('昆明市', '云南省'), \
            ('福建省', '中国'), \
            ('福州市', '福建省'), \
            ('陕西省', '中国'), \
            ('西安市', '陕西省'), \
            ('西藏自治区', '中国'), \
            ('拉萨市', '西藏自治区'), \
            ('甘肃省', '中国'), \
            ('兰州市', '甘肃省'), \
            ('青海省', '中国'), \
            ('西宁市', '青海省'), \
            ('宁夏回族自治区', '中国'), \
            ('银川市', '宁夏回族自治区'), \
            ('新疆维吾尔自治区', '中国'), \
            ('乌鲁木齐市', '新疆维吾尔自治区'), \
            )

for x in AD_PLACE:
    r = AdPlace.objects.filter(place_name__exact=x[1]) #@UndefinedVariable
    if r.count() >= 1 :
        print u'Add Place %s to %s' % (unicode(x[0], DEFAULT_CHARSET), unicode(x[1], DEFAULT_CHARSET))
        ad_place = AdPlace(place_name=unicode(x[0], DEFAULT_CHARSET), place_parent=r[0])
        ad_place.save()
    else: 
        print u'Add Place %s' % unicode(x[0], DEFAULT_CHARSET)
        ad_place = AdPlace(place_name=x[0], place_parent=None)
        ad_place.save()
print 'Create Place item Done.'

print 'Create UserGroup'

print 'Create administrator user'
admin_user = User.objects.create_user(username=ADMIN_NAME, email=ADMIN_EMAIL, password=ADMIN_PASSWD+'123')
admin_user.is_staff =True
admin_user.is_active = True
admin_user.is_superuser = True
admin_user.save()

admin_profile = AdUserProfile(
                              user=admin_user, \
                              user_desc='First add user', \
                              user_expire_date=datetime.date(2038, 12, 30), \
                              user_type='manager', \
                              user_place = AdPlace.objects.get(place_name__exact='中国') #@UndefinedVariable
                              )

admin_profile.save()
print 'Create administrator user Done'

print 'Create UserGroup'
ad_group_corp_administrators = Group(name='corp_administrators')
ad_group_corp_administrators.save()
ad_group_corp_administrators.permissions.add(
                                             Permission.objects.get(codename='can_add_place'), \
                                             Permission.objects.get(codename='can_del_place'), \
                                             Permission.objects.get(codename='can_edit_place'), \
                            
                                             Permission.objects.get(codename='can_add_aditem'), \
                                             Permission.objects.get(codename='can_del_aditem'), \
                                             Permission.objects.get(codename='can_edit_aditem'), \
                                             Permission.objects.get(codename='can_approve_aditem'), \
                                             Permission.objects.get(codename='can_change_aditem_expire'), \
                                             
                                             Permission.objects.get(codename='can_add_adorg'), \
                                             Permission.objects.get(codename='can_edit_adorg'), \
                                             Permission.objects.get(codename='can_del_adorg'), \
                                             
                                             Permission.objects.get(codename='can_add_adpush'), \
                                             Permission.objects.get(codename='can_edit_adpush'), \
                                             Permission.objects.get(codename='can_del_adpush'), \
                                             Permission.objects.get(codename='can_approve_adpush'), \
                                             Permission.objects.get(codename='can_change_adpush_expire'), \
                                             
                                             Permission.objects.get(codename='can_add_userprofile'), \
                                             Permission.objects.get(codename='can_del_userprofile'), \
                                             Permission.objects.get(codename='can_edit_userprofile'), \
                                             Permission.objects.get(codename='can_change_user_state'), \
                                             Permission.objects.get(codename='can_change_user_expire'), \
                                             )

# create corp manager group
ad_group_corp_managers = Group(name='corp_managers')
ad_group_corp_managers.save()
ad_group_corp_managers.permissions.add(
                            Permission.objects.get(codename='can_add_place'), \
                            Permission.objects.get(codename='can_del_place'), \
                            Permission.objects.get(codename='can_edit_place'), \
                               
                            Permission.objects.get(codename='can_add_aditem'), \
                            Permission.objects.get(codename='can_del_aditem'), \
                            Permission.objects.get(codename='can_edit_aditem'), \
                            Permission.objects.get(codename='can_approve_aditem'), \
                            Permission.objects.get(codename='can_change_aditem_expire'), \
                            
                             
                            Permission.objects.get(codename='can_add_adorg'), \
                            Permission.objects.get(codename='can_edit_adorg'), \
                            Permission.objects.get(codename='can_del_adorg'), \
                             
                            Permission.objects.get(codename='can_add_adpush'), \
                            Permission.objects.get(codename='can_edit_adpush'), \
                            Permission.objects.get(codename='can_del_adpush'), \
                            Permission.objects.get(codename='can_approve_adpush'), \
                            Permission.objects.get(codename='can_change_adpush_expire'), \
                            
                            Permission.objects.get(codename='can_add_userprofile'), \
                            Permission.objects.get(codename='can_del_userprofile'), \
                            Permission.objects.get(codename='can_edit_userprofile'), \
                            Permission.objects.get(codename='can_change_user_state'), \
                            Permission.objects.get(codename='can_change_user_expire'), \
                            )

# create corp user group
ad_group_corp_users = Group(name='corp_users')
ad_group_corp_users.save()
ad_group_corp_users.permissions.add(
                            #Permission.objects.get(codename='can_add_place'), \
                            #Permission.objects.get(codename='can_del_place'), \
                            #Permission.objects.get(codename='can_edit_place'), \
                            
                            #Permission.objects.get(codename='can_add_user_to_group'), \
                            #Permission.objects.get(codename='can_del_user_to_group'), \
                            #Permission.objects.get(codename='can_edit_user_to_group'), \
                               
                            Permission.objects.get(codename='can_add_aditem'), \
                            #Permission.objects.get(codename='can_del_aditem'), \
                            Permission.objects.get(codename='can_edit_aditem'), \
                            #Permission.objects.get(codename='can_approve_aditem'), \
                            #Permission.objects.get(codename='can_change_aditem_expire'), \
                            
                             
                            Permission.objects.get(codename='can_add_adorg'), \
                            Permission.objects.get(codename='can_edit_adorg'), \
                            #Permission.objects.get(codename='can_del_adorg'), \
                             
                            Permission.objects.get(codename='can_add_adpush'), \
                            Permission.objects.get(codename='can_edit_adpush'), \
                            #Permission.objects.get(codename='can_del_adpush'), \
                            #Permission.objects.get(codename='can_approve_adpush'), \
                            #Permission.objects.get(codename='can_change_adpush_expire'), \
                            
                            #Permission.objects.get(codename='can_add_userprofile'), \
                            #Permission.objects.get(codename='can_del_userprofile'), \
                            #Permission.objects.get(codename='can_edit_userprofile'), \
                            #Permission.objects.get(codename='can_change_user_state'), \
                            #Permission.objects.get(codename='can_change_user_expire'), \
                             )

# create agent group province
ad_group = Group(name='province_agents')
ad_group.save()
ad_group.permissions.add(
                            #Permission.objects.get(codename='can_add_place'), \
                            #Permission.objects.get(codename='can_del_place'), \
                            #Permission.objects.get(codename='can_edit_place'), \
                            
                            #Permission.objects.get(codename='can_add_user_to_group'), \
                            #Permission.objects.get(codename='can_del_user_to_group'), \
                            #Permission.objects.get(codename='can_edit_user_to_group'), \
                               
                            Permission.objects.get(codename='can_add_aditem'), \
                            #Permission.objects.get(codename='can_del_aditem'), \
                            Permission.objects.get(codename='can_edit_aditem'), \
                            #Permission.objects.get(codename='can_approve_aditem'), \
                            #Permission.objects.get(codename='can_change_aditem_expire'), \
                            
                             
                            Permission.objects.get(codename='can_add_adorg'), \
                            Permission.objects.get(codename='can_edit_adorg'), \
                            #Permission.objects.get(codename='can_del_adorg'), \
                             
                            Permission.objects.get(codename='can_add_adpush'), \
                            Permission.objects.get(codename='can_edit_adpush'), \
                            #Permission.objects.get(codename='can_del_adpush'), \
                            #Permission.objects.get(codename='can_approve_adpush'), \
                            #Permission.objects.get(codename='can_change_adpush_expire'), \
                            
                            #Permission.objects.get(codename='can_add_userprofile'), \
                            #Permission.objects.get(codename='can_del_userprofile'), \
                            #Permission.objects.get(codename='can_edit_userprofile'), \
                            #Permission.objects.get(codename='can_change_user_state'), \
                            #Permission.objects.get(codename='can_change_user_expire'), \
                            )

# create agent group city
ad_group = Group(name='city_agents')
ad_group.save()
ad_group.permissions.add(
                            #Permission.objects.get(codename='can_add_place'), \
                            #Permission.objects.get(codename='can_del_place'), \
                            #Permission.objects.get(codename='can_edit_place'), \
                            
                            #Permission.objects.get(codename='can_add_user_to_group'), \
                            #Permission.objects.get(codename='can_del_user_to_group'), \
                            #Permission.objects.get(codename='can_edit_user_to_group'), \
                               
                            Permission.objects.get(codename='can_add_aditem'), \
                            #Permission.objects.get(codename='can_del_aditem'), \
                            Permission.objects.get(codename='can_edit_aditem'), \
                            #Permission.objects.get(codename='can_approve_aditem'), \
                            #Permission.objects.get(codename='can_change_aditem_expire'), \
                            
                             
                            Permission.objects.get(codename='can_add_adorg'), \
                            Permission.objects.get(codename='can_edit_adorg'), \
                            #Permission.objects.get(codename='can_del_adorg'), \
                             
                            Permission.objects.get(codename='can_add_adpush'), \
                            Permission.objects.get(codename='can_edit_adpush'), \
                            #Permission.objects.get(codename='can_del_adpush'), \
                            #Permission.objects.get(codename='can_approve_adpush'), \
                            #Permission.objects.get(codename='can_change_adpush_expire'), \
                            
                            #Permission.objects.get(codename='can_add_userprofile'), \
                            #Permission.objects.get(codename='can_del_userprofile'), \
                            #Permission.objects.get(codename='can_edit_userprofile'), \
                            #Permission.objects.get(codename='can_change_user_state'), \
                            #Permission.objects.get(codename='can_change_user_expire'), \
                            )

# create agent group zone
ad_group = Group(name='place_agents')
ad_group.save()
ad_group.permissions.add(
                            #Permission.objects.get(codename='can_add_place'), \
                            #Permission.objects.get(codename='can_del_place'), \
                            #Permission.objects.get(codename='can_edit_place'), \
                            
                            #Permission.objects.get(codename='can_add_user_to_group'), \
                            #Permission.objects.get(codename='can_del_user_to_group'), \
                            #Permission.objects.get(codename='can_edit_user_to_group'), \
                               
                            Permission.objects.get(codename='can_add_aditem'), \
                            Permission.objects.get(codename='can_del_aditem'), \
                            #Permission.objects.get(codename='can_edit_aditem'), \
                            #Permission.objects.get(codename='can_approve_aditem'), \
                            #Permission.objects.get(codename='can_change_aditem_expire'), \
                            
                             
                            Permission.objects.get(codename='can_add_adorg'), \
                            Permission.objects.get(codename='can_edit_adorg'), \
                            #Permission.objects.get(codename='can_del_adorg'), \
                             
                            Permission.objects.get(codename='can_add_adpush'), \
                            Permission.objects.get(codename='can_edit_adpush'), \
                            #Permission.objects.get(codename='can_del_adpush'), \
                            #Permission.objects.get(codename='can_approve_adpush'), \
                            #Permission.objects.get(codename='can_change_adpush_expire'), \
                            
                            #Permission.objects.get(codename='can_add_userprofile'), \
                            #Permission.objects.get(codename='can_del_userprofile'), \
                            #Permission.objects.get(codename='can_edit_userprofile'), \
                            #Permission.objects.get(codename='can_change_user_state'), \
                            #Permission.objects.get(codename='can_change_user_expire'), \
                            )

# create ad client group
ad_group = Group(name='ad_clients')
ad_group.save()

# create ad player group
ad_group = Group(name='ad_players')
ad_group.save()
print 'Create Usergroup Done'

print '''Add administrator's permissions'''
admin_user.groups= (ad_group_corp_administrators, ad_group_corp_managers)
admin_user.save()

# auto add default ad org
print 'Create default ad organizes'
for x in AdPlace.objects.all(): #@UndefinedVariable
    orginfo_obj = AdOrganizeInfo(creator=admin_user)
    orginfo_obj.save()
    
    org_obj = AdOrganize(info=orginfo_obj)
    org_obj.save()
    org_obj.places.add(x)
    # if add or update places user , need to reflush AdOrgnaizeInfo 
    org_obj.flush_adorg_info()

print 'Create default ad organizes Done.'

print 'Initialization Done'
